package deliciouse.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import deliciouse.biz.NewsBiz;
import deliciouse.biz.impl.NewsBizImpl;
import deliciouse.entity.News;

@WebServlet ("/addNewsManegement")
public class NewsManegementAddServlet extends HttpServlet{

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("UTF-8");
			
			int id = 0;
			if(request.getParameter("id")!=null && !request.getParameter("id").isEmpty())
				id= Integer.valueOf(request.getParameter("id"));
			System.out.println(id);
			int add = 0;
			String title = null;
			title = request.getParameter("title");
			String content = null;
			content = request.getParameter("content");
			Date publishdate =  new Date();
			News newsupdate = new News(title,content,publishdate,id);
			News newsadd = new News(title,content,publishdate);
			NewsBiz newsbizo = new NewsBizImpl();
			List<News> list=newsbizo.getall();
			for(News news : list){
				if(id==news.getId()){
					add=1;
					//newsdao.updateNews(title, content,publishdate,id);				
					break;
					//response.sendRedirect("newslistmanegement");
				}else{

					//newsdao.addnews(title, content, publishdate);
					add =2;
					//response.sendRedirect("newslistmanegement");
				}
			}
			switch (add) {
			case 1:
				newsbizo.updateNews(newsupdate);
				break;
			case 2:
				newsbizo.addnews(newsadd);
				break;
			}
			
			response.sendRedirect("newslistmanegement");
			
			
	}

}
